package com.yunupmall.service.ford.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunupmall.api.fordVO.AddressVo;
import com.yunupmall.api.fordentity.Address;
import com.yunupmall.entity.admin.memberpojo.Member;
import com.yunupmall.mapper.FordMapper.AddressMapper;
import com.yunupmall.service.ford.service.AddressService;
import com.yunupmall.uits.IsMobileUtils;
import com.yunupmall.uits.SessionUtis;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class Addressimpl extends ServiceImpl<AddressMapper, Address> implements AddressService {
    /**
     * 新增收获地址
     */
    @Override
    @Transactional
    public String save(AddressVo addressVo) {
        if (ObjectUtil.isEmpty(addressVo.getProvince())){
            throw new RuntimeException("省级信息为空");
        }
        if (ObjectUtil.isEmpty(addressVo.getCity())){
            throw new RuntimeException("市级信息为空");
        }
        if (ObjectUtil.isEmpty(addressVo.getCounty())){
            throw new RuntimeException("区级信息为空");
        }
        if (ObjectUtil.isEmpty(addressVo.getAddress())){
            throw new RuntimeException("详细地址为空");
        }
        if (ObjectUtil.isEmpty(addressVo.getName())){
            throw new RuntimeException("姓名为空");
        }
        if (ObjectUtil.isEmpty(addressVo.getPhone())){
            throw new RuntimeException("电话号码为空");
        }
        if (!IsMobileUtils.isPhone(addressVo.getPhone())){
            if(!IsMobileUtils.isMobile(addressVo.getPhone())){
                throw new RuntimeException("电话号码格式错误");
            }
        }
        Member membet = (Member) SessionUtis.getSessionAttribute("member");
        if (ObjectUtil.isEmpty(membet)) {
            throw new RuntimeException("用户信息为空");
        }
        Address address = new Address();
        BeanUtils.copyProperties(addressVo, address);
        address.setMId(membet.getId());
        address.setCreateTime(LocalDateTime.now());
        address.setIsDefault(1);
        address.setDeflag(0);
        if (this.save(address)) {
            return "创建成功";
        }else {
            throw new RuntimeException("创建失败");
        }
    }

    /**
     * 获取地址信息
     * @param addressVo
     * @return
     */
    @Override
    public List<AddressVo> getList(AddressVo addressVo) {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();

            if (ObjectUtil.isEmpty(addressVo.getMId())){
                Member membet = (Member) SessionUtis.getSessionAttribute("member");
                if (ObjectUtil.isEmpty(membet)) {
                    throw new RuntimeException("用户信息为空");
                }
                wrapper.eq(Address ::getMId,membet.getId());
            }else {
                wrapper.eq(Address ::getMId,addressVo.getMId());
            }
        if (ObjectUtil.isNotEmpty(addressVo.getId())){
            wrapper.eq(Address ::getId,addressVo.getId());
        }
        wrapper.eq(Address ::getDeflag,0);
        List<Address> list = this.list(wrapper);
        final AddressVo[] addressVo1 = new AddressVo[1];
        if (list.size()!=0){
            List<AddressVo> addressVoArrayList = new ArrayList<>();
            list.forEach(address -> {
                StringBuilder stringBuilder = new StringBuilder();
                addressVo1[0] = new AddressVo();
              BeanUtils.copyProperties(address, addressVo1[0]);
              stringBuilder.append(address.getProvince()+"|").append(address.getCity()+"|").append(address.getCounty());
              addressVo1[0].setRegion(stringBuilder.toString());
              addressVoArrayList.add(addressVo1[0]);
            });
            List<AddressVo> collect = addressVoArrayList.stream().sorted(Comparator.comparing(AddressVo::getIsDefault)).collect(Collectors.toList());
            return collect;
        }
        return null;
    }

    /**
     * 删除地址信息
     *
     */
    @Override
    @Transactional
    public String del(Integer id) {
        if (ObjectUtil.isEmpty(id)) {
            throw new RuntimeException("id为空");
        }
        Address address = new Address();
        address.setId(id);
        address.setDeflag(1);
        if (this.updateById(address)){
            return "删除成功";
        }else {
            throw new RuntimeException("删除失败");
        }
    }

    /**
     * 设为默认地址
     */
    @Override
    @Transactional
    public String updateisDefault(AddressVo addressVo) {
        if (ObjectUtil.isEmpty(addressVo.getId())) {
            throw new RuntimeException("Id为空");
        }
        Member member =(Member) SessionUtis.getSessionAttribute("member");
        if (ObjectUtil.isEmpty(member)){
            throw new RuntimeException("用户信息为空");
        }
        LambdaUpdateWrapper<Address> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Address ::getMId,member.getId());
        wrapper.eq(Address ::getDeflag,0);
        Address address = new Address();
        address.setIsDefault(1);
        if (this.update(address,wrapper)){
            Address address1 = new Address();
            BeanUtils.copyProperties(addressVo, address1);
            if (this.updateById(address1)){
                return "修改成功";
            }
        }else {
            throw new RuntimeException("修改失败");
        }
        return null;
    }

    /**
     * 不一定
     * @param addressVo
     * @return
     */
    @Override
    public List<AddressVo> byid(AddressVo addressVo) {
        if (ObjectUtil.isEmpty(addressVo.getId())){
            throw new RuntimeException("ID为空");
        }
        Address byId = this.getById(addressVo.getId());
        AddressVo addressVo1 = new AddressVo();
        BeanUtils.copyProperties(byId,addressVo1);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(byId.getProvince()+"|").append(byId.getCity()+"|").append(byId.getCounty() + "|");
        addressVo1.setRegion(stringBuilder.toString());
        List<AddressVo> addressVos = new ArrayList<>();
        addressVos.add(addressVo1);
        return addressVos;
    }
}
